<template>
  <el-dialog
    :title="!form.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    width="500px"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="form"
      size="mini"
      @keyup.enter.native="submitForm()"
      label-width="80px"
    >
      <el-form-item label="商品" prop="skuId">
        <sku-select v-model="form.skuId"></sku-select>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="秒杀总量" prop="count">
            <el-input-number v-model="form.count" :min="1" placeholder="秒杀总量"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="限购数量" prop="limited">
            <el-input-number v-model="form.limited" :min="1" placeholder="限购数量"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="秒杀价格" prop="price">
            <el-input-number v-model="form.price" :min="0" :precision="2" :step="0.1" placeholder="秒杀价格"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="sort">
            <el-input v-model="form.sort" placeholder="排序"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" icon="el-icon-circle-check" type="primary" @click="submitForm()">确定</el-button>
      <el-button size="mini" icon="el-icon-circle-close" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getSeckillSkuRelation, saveSeckillSkuRelation, updateSeckillSkuRelation} from "@/api/coupon/seckillSkuRelation";
import SkuSearchSelect from "@/views/common/skuSearchSelect.vue";
import SkuSelect from "@/views/common/skuSelect.vue";

export default {
  components: {SkuSelect, SkuSearchSelect},
  data() {
    return {
      visible: false,
      form: {},
      rules: {
        skuId: [
          {required: true, message: "请选择商品", trigger: "blur"}
        ],
        price: [
          {required: true, message: "请输入价格", trigger: "blur"}
        ],
        count: [
          {required: true, message: "请输入数量", trigger: "blur"}
        ],
        limited: [
          {required: true, message: "请输入限购数量", trigger: "blur"}
        ]
      }
    };
  },
  props: {
    seckillId: {
      type: String,
      default: ''
    }
  },
  methods: {
    edit(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["form"].resetFields();
        getSeckillSkuRelation(id).then(res => {
          if (res.success) {
            this.form = res.data
          }
        });
      });
    },
    add() {
      this.form = {
        sort: 999,
      }
      this.visible = true
    },
    // 表单提交
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.seckillId = this.seckillId
        if (valid) {
          if (!this.form.id) {
            saveSeckillSkuRelation(this.form).then(res => {
              if (res.success) {
                this.visible = false;
                this.$emit("ok");
                this.$message.success('新增成功')
              }
            })
          } else {
            updateSeckillSkuRelation(this.form).then(res => {
              if (res.success) {
                this.visible = false;
                this.$emit("ok");
                this.$message.success('修改成功')
              }
            })
          }
        }
      });
    }
  }
};
</script>
